﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sudoku;
using MySql.Data.MySqlClient;

namespace DAL
{
    partial class DAL
    {
        public class RekordDAO : IDaoCrud<Rekord>
        {
            #region IDaoCrud<Rekord> Members
            protected MySqlCommand c;
            public long create(Rekord entity)
            {
                try
                {
                    c = new MySqlCommand("insert into rekord (vrijeme_sec, ime, prezime, level, datum) values (@Vrijeme, @Ime, @Prezime, @Level, @Datum);", con);
                    c.Parameters.AddWithValue("@Vrijeme", entity.Vrijeme);
                    c.Parameters.AddWithValue("@Ime", entity.Ime);
                    c.Parameters.AddWithValue("@Prezime", entity.Prezime);
                    c.Parameters.AddWithValue("@Level", entity.Level);
                    c.Parameters.AddWithValue("@Datum", entity.Datum);
                    c.ExecuteNonQuery();
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Rekord> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from rekord ORDER BY vrijeme_sec;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Rekord> rekordi = new List<Rekord>();
                    while (r.Read())
                    {
                        Rekord rekord = new Rekord();
                        rekord.Id = r.GetInt32("id");
                        rekord.Vrijeme = r.GetInt32("vrijeme_sec");
                        rekord.Ime = r.GetString("ime");
                        rekord.Prezime = r.GetString("prezime");
                        rekord.Level = r.GetString("level");
                        rekord.Datum = r.GetDateTime("datum");
                        rekordi.Add(rekord);
                    }
                    return rekordi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Rekord read(Rekord entity)
            {
                try
                {
                    c = new MySqlCommand("select * from rekord where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.Id);
                    MySqlDataReader r = c.ExecuteReader();
                    Rekord rekord = new Rekord();
                    if (r.Read())
                    {
                        rekord.Id = r.GetInt32("id");
                        rekord.Vrijeme = r.GetInt32("vrijeme_sec");
                        rekord.Ime = r.GetString("ime");
                        rekord.Prezime = r.GetString("prezime");
                        rekord.Level = r.GetString("level");
                        rekord.Datum = r.GetDateTime("datum");
                    }
                    return rekord;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Rekord update(Rekord entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE rekord SET vrijeme_sec = @Vrijeme, ime = @Ime, prezime = @Prezime, level = @Level, datum = @Datum where id = @ID;", con);
                    c.Parameters.AddWithValue("@Vrijeme", entity.Vrijeme);
                    c.Parameters.AddWithValue("@Ime", entity.Ime);
                    c.Parameters.AddWithValue("@Prezime", entity.Prezime);
                    c.Parameters.AddWithValue("@Level", entity.Level);
                    c.Parameters.AddWithValue("@Datum", entity.Datum);
                    c.Parameters.AddWithValue("@ID", entity.Id);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Rekord entity)
            {
                try
                {
                    c = new MySqlCommand("delete from rekord where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.Id);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Rekord getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from rekord where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Rekord rekord = new Rekord();
                    if (r.Read())
                    {
                        rekord.Id = r.GetInt32("id");
                        rekord.Vrijeme = r.GetInt32("vrijeme_sec");
                        rekord.Ime = r.GetString("ime");
                        rekord.Prezime = r.GetString("prezime");
                        rekord.Level = r.GetString("level");
                        rekord.Datum = r.GetDateTime("datum");
                    }
                    return rekord;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Rekord> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from rekord where @Kolona = @Vrijednost;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Rekord> rekordi = new List<Rekord>();
                    while (r.Read())
                    {
                        Rekord rekord = new Rekord();
                        rekord.Id = r.GetInt32("id");
                        rekord.Vrijeme = r.GetInt32("vrijeme_sec");
                        rekord.Ime = r.GetString("ime");
                        rekord.Prezime = r.GetString("prezime");
                        rekord.Level = r.GetString("level");
                        rekord.Datum = r.GetDateTime("datum");
                        rekordi.Add(rekord);
                    }
                    return rekordi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
